package com.nopay.dxdwphone.baiduocr;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import com.blankj.utilcode.util.RegexUtils;
import com.nopay.dxdwphone.baiduocr.bean.OCRResponse;
import com.nopay.dxdwphone.baiduocr.utils.Base64Util;
import com.nopay.dxdwphone.baiduocr.utils.FileUtil;
import com.nopay.dxdwphone.baiduocr.utils.GsonUtils;
import com.nopay.dxdwphone.baiduocr.utils.HttpUtil;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class OCRAPI {

//    public static final String HOST = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";
    public static final String HOST_NUMBERS = "https://aip.baidubce.com/rest/2.0/ocr/v1/numbers";

    public void getPhoneByPic(final String picFilePath, OnOCRResultCallback callback) {
        this.mCallback = callback;
        new Thread(() -> {
            String token = OCRTokenAPI.getToken();
            if(token != null) {
                try {
                    byte[] imgData = FileUtil.readFileByBytes(picFilePath);
                    String imgStr = Base64Util.encode(imgData);
                    String imgParam = URLEncoder.encode(imgStr, "UTF-8");
                    String param = "image=" + imgParam;
//                    int hostId = SPUtils.getInstance().getInt("HOST_ID", 0);
//                    String result = HttpUtil.post(hostId == 0 ? HOST : HOST_NUMBERS, token, param);
                    String result = HttpUtil.post(HOST_NUMBERS, token, param);
                    System.out.println("识别结果：" + result);
                    OCRResponse response = GsonUtils.fromJson(result, OCRResponse.class);
//                    if(response.getError_code() == 17 || response.getError_code() == 19) {
//                        SPUtils.getInstance().put("HOST_ID", hostId == 0 ? 1 : 0);
//                        getPhoneByPic(picFilePath, callback);
//                        return;
//                    }

                    Message msg = Message.obtain();
                    msg.what = 1;
                    msg.obj = response;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = 0;
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            // todo 处理识别结果，返回手机号的列表
            List<OCRResponse.Word> wordList = new ArrayList<>();
            if(msg.what == 1) {
                OCRResponse response = msg.obj == null ? null : (OCRResponse) msg.obj;
                if(response != null && response.getWords_result() != null) {
                    for (int i = 0; i < response.getWords_result().size(); i++) {
                        OCRResponse.Word word = response.getWords_result().get(i);
                        if(RegexUtils.isMobileSimple(word.getWords())) wordList.add(word);
                    }
                }
            }
            if(mCallback != null) {
                mCallback.onCallback(wordList);
            }
            mCallback = null;
        }
    };

    private OnOCRResultCallback mCallback;

    public interface OnOCRResultCallback {

        void onCallback(List<OCRResponse.Word> wordList);
    }

}
